﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using 刘颜红销售项目管理.Business.DOM ;
using XDesigner.ORM;
using 刘颜红销售项目管理.Business;
using System.Collections;


namespace 刘颜红销售项目管理
{
    public partial class frm人员列表 : DevExpress.XtraEditors.XtraForm
    {
        public frm人员列表()
        {
            InitializeComponent();
            strTitle = this.Text;
        }
        
        private string strTitle = null;

        private Class工作单位 _当前单位 = null;

        public Class工作单位 当前单位
        {
            get { return _当前单位; }
            set { _当前单位 = value; cboUnit.EditValue = value; }
        }

        public void RefreshView()
        {
            Class工作单位 unit = (Class工作单位)cboUnit.EditValue;
            ORMBindingSource ds = null;
            if (unit == 未指定单位)
            {
                ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(typeof(Class人员基本信息), MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class人员基本信息)));
            }
            else
            {
                ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(typeof(Class人员基本信息), unit.人员列表);
            }
            ds.LogItemChange = true;
            ds.Changed += new EventHandler(ds_Changed);
            myGridControl.DataSource = ds;
        }

        void ds_Changed(object sender, EventArgs e)
        {
            this.Text = strTitle + "*";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Append);
            //DataGridViewRow row = myGridControl myDataGridView.Rows[ myDataGridView.NewRowIndex ] ;
            //row.Selected = true ;
            //myDataGridView.CurrentCell = row.Cells[1];
            //myDataGridView.BeginEdit(false );
        }

        private Class工作单位 未指定单位 = new Class工作单位();
        private void frm单位基本信息_Load(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            未指定单位.单位名称 = "未指定";
            list.Add(未指定单位);
            list.AddRange(MyApplicationFramework.Instance.单位列表);
            cboUnit.Properties.DataSource = list;
            if (this.当前单位 == null)
                this.cboUnit.EditValue = 未指定单位;
            else
                this.cboUnit.EditValue = this.当前单位;
            myUnitList.DataSource = MyApplicationFramework.Instance.单位列表;
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Remove);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource)myGridControl.DataSource;
            if (ds.Modified)
            {
                if (MessageBox.Show(
                    this,
                    "记录已经修改，是否保存?",
                    "系统提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                }
            }
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource) this.myGridControl.DataSource;
            if (ds.Modified)
            {
                ds.Engine.OpenConnection();
                int result = ds.Update();
                ds.Engine.CloseConnection();
                ds.Engine.Buffer.Clear(typeof(Class工作单位));
                this.Text = strTitle;
                MessageBox.Show("更新了 " + result + " 条记录.");
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshView();
        }

        //public class 工作单位RelationList : ORMBindingSource , DevExpress.Data.IRelationList
        //{
        //    #region IRelationList 成员

        //    public System.Collections.IList GetDetailList(int index, int relationIndex)
        //    {
        //        Class工作单位 item = (Class工作单位)this.List[index];
        //        return item.人员列表;
        //    }

        //    public string GetRelationName(int index, int relationIndex)
        //    {
        //        return "工作人员";
        //    }

        //    public bool IsMasterRowEmpty(int index, int relationIndex)
        //    {
        //        return false;
        //    }

        //    public int RelationCount
        //    {
        //        get { return 1; }
        //    }

        //    #endregion
        //}
         
    }
}